package com.wonderland.butterfly.autoupdate.model;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

/**
 * Created by gaoxinyu on 2015-07-21 0021.
 */
public class Patch {

    private String databasePatchDir;
    private String patchDir;
    private String originalVersion;
    private String targetVersion;
    private String pomFilePath;
    private String warFileDir;
    private String warName;
    private String profile;
    private String patchName;


    public Patch(String databasePatchDir, String patchDir, String originalVersion, String originalVersionUrl, String targetVersion,
                 String pomFilePath, String profile, String warFileDir, String warName) throws IOException {
        this.databasePatchDir = databasePatchDir;
        this.patchDir = patchDir;
        if (originalVersion.equals("")) {
            this.originalVersion = new BufferedReader(new InputStreamReader(new URL(originalVersionUrl).openConnection().getInputStream(), "utf-8")).readLine();
        } else {
            this.originalVersion = originalVersion;
        }
        this.targetVersion = targetVersion;
        this.pomFilePath = pomFilePath;
        this.profile = profile;
        this.warFileDir = warFileDir;
        this.patchName = "patch" + new SimpleDateFormat("yyyyMMdd_HHmm").format(new Date());
        this.warName = warName;
    }

    public Patch(Properties p) throws IOException {
        this(p.getProperty("databasePatchDir"), p.getProperty("patchDir"), p.getProperty("originalVersion"),
                p.getProperty("originalVersionUrl"), p.getProperty("targetVersion"), p.getProperty("pomFilePath"),
                p.getProperty("profile"), p.getProperty("warFileDir"), p.getProperty("warName"));
    }

    public String getGeneratedWarPath() {
        return warFileDir + warName;
    }

    public String getPatchWarPath() {
        return patchDir + patchName + File.separator + warName;
    }

    public String getOriginalVersion() {
        return originalVersion;
    }

    public void setOriginalVersion(String originalVersion) {
        this.originalVersion = originalVersion;
    }

    public String getTargetVersion() {
        return targetVersion;
    }

    public void setTargetVersion(String targetVersion) {
        this.targetVersion = targetVersion;
    }

    public String getPomFilePath() {
        return pomFilePath;
    }

    public void setPomFilePath(String pomFilePath) {
        this.pomFilePath = pomFilePath;
    }

    public String getWarFileDir() {
        return warFileDir;
    }

    public void setWarFileDir(String warFileDir) {
        this.warFileDir = warFileDir;
    }

    public String getDatabasePatchDir() {
        return databasePatchDir;
    }

    public void setDatabasePatchDir(String databasePatchDir) {
        this.databasePatchDir = databasePatchDir;
    }

    public String getPatchDir() {
        return patchDir;
    }

    public void setPatchDir(String patchDir) {
        this.patchDir = patchDir;
    }

    public String getProfile() {
        return profile;
    }

    public void setProfile(String profile) {
        this.profile = profile;
    }

    public String getPatchName() {
        return patchName;
    }

    public void setPatchName(String patchName) {
        this.patchName = patchName;
    }

    public String getWarName() {
        return warName;
    }

    public void setWarName(String warName) {
        this.warName = warName;
    }
}
